﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class WxRedPacketsList
    {
        public DataSet GetList(string where) {
            StringBuilder builder = new StringBuilder();
            builder.Append("select *");
            builder.Append(" FROM WxRedPacketsList ");
            if (where.Trim() != "")
            {
                builder.Append(" where " + where);
            }
            return DbHelperSQL.Query(builder.ToString());
        
        
        }
        public DataSet GetList_Count(string where)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(*) as counts ");
            builder.Append(" FROM WxRedPacketsList ");
            if (where.Trim() != "")
            {
                builder.Append(" where " + where);
            }
            return DbHelperSQL.Query(builder.ToString());


        }
        public decimal GetList_summoney(string where)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select isnull(sum(isnull(money,0)),0) as counts ");
            builder.Append(" FROM WxRedPacketsList ");
            if (where.Trim() != "")
            {
                builder.Append(" where " + where);
            }
            return (decimal)DbHelperSQL.Query(builder.ToString()).Tables[0].Rows[0][0];


        }
        public int Add(Chain.Model.WxRedPacketsList model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into WxRedPacketsList(");
            builder.Append("money,SerialNumber,datetime,memid,state,msg,type,openid,status,OperatorId,send_listid)");
            builder.Append(" values (");
            builder.Append("@money,@SerialNumber,@datetime,@memid,@state,@msg,@type,@openid,@status,@OperatorId,@send_listid)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { 
                new SqlParameter("@money", SqlDbType.Money, 8)
                , new SqlParameter("@SerialNumber", SqlDbType.NVarChar, 50)
                , new SqlParameter("@datetime", SqlDbType.DateTime)
            , new SqlParameter("@memid", SqlDbType.Int, 4),
            new SqlParameter("@state", SqlDbType.Int, 4)
            , new SqlParameter("@msg", SqlDbType.NVarChar, 50)
            , new SqlParameter("@type", SqlDbType.Int, 50)
            , new SqlParameter("@openid", SqlDbType.NVarChar, 50)
            , new SqlParameter("@status", SqlDbType.NVarChar, 50)
             , new SqlParameter("@OperatorId", SqlDbType.Int, 4)
             , new SqlParameter("@send_listid", SqlDbType.NVarChar, 50)
            };
            cmdParms[0].Value = model.money;
            cmdParms[1].Value = model.SerialNumber;
            cmdParms[2].Value = model.datetime;
            cmdParms[3].Value = model.memid;
            cmdParms[4].Value = model.state;
            cmdParms[5].Value = model.msg;
            cmdParms[6].Value = model.type;
            cmdParms[7].Value = model.openid;
            cmdParms[8].Value = model.status;
            cmdParms[9].Value = model.OperatorId;
            cmdParms[10].Value = model.send_listid;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public DataSet GetWxRedPocketLog(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "WxRedPacketsList,Mem,SysShop,SysUser";
            string[] column = new string[] { "WxRedPacketsList.*,Mem.MemCard,Mem.MemName,SysUser.UserName,SysShop.ShopName" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "ID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }
    }
}

